package Core;

import Logic.BaseCommand;
import Logic.CommandId;
import Logic.CommandProcessor;

import java.lang.reflect.Method;
import java.util.HashMap;

public class ProtoCenter {
    private static HashMap<Integer, ProtoProcessor> protoProcessorMap = new HashMap<>();

    public static void Dispatch(int id, NetData nd) throws Exception {
        ProtoProcessor pp = protoProcessorMap.get(id);
        if (pp != null) {
            Method method = null;
            try {
                method = pp.o.getClass().getMethod(pp.methodName, NetData.class);
            } catch (NoSuchMethodException e) {
                method = pp.o.getClass().getDeclaredMethod(pp.methodName, NetData.class);
                method.setAccessible(true);
            }
            method.invoke(pp.o, nd);
        }
    }

    public static void RegisterProtoProcessor(int id, Object o, String methodName) {
        ProtoProcessor pp = new ProtoProcessor(o, methodName);
        protoProcessorMap.put(id, pp);
    }
}
